package com.zhu.responder.websocker.netty;

import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;

public class NettyServerApplication implements CommandLineRunner {
 
    @Value("${server.port}")
    private int serverPort;
 
    private NettyServer nettyServer;
 
    public NettyServerApplication(NettyServer nettyServer){
        this.nettyServer=nettyServer;
    }
 
    @Override
    public void run(String... args) throws Exception {
        //netty服务为springboot项目端口号加50
        ChannelFuture channelFuture = nettyServer.start(serverPort + 50);
        //添加钩子函数，jvm关闭时一并将注册的服务也关闭
        Runtime.getRuntime().addShutdownHook(new Thread(nettyServer::destory));
        //对关闭通道事件进行监听
        channelFuture.channel().closeFuture().sync();
    }
}